package com.archermind.style.util;

import java.io.Serializable;

/**
 * @author ghostsf
 * @see
 * @since
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 6547445432157126924L;

    private static final int SUCCESS = 0;

    private static final int ERROR = -1;

    /**
     * 标示
     */
    private int code;

    /**
     * 返回给页面的成功消息
     */
    private String msg;

    /**
     * 返回的内容
     */
    private T data;

    public Result() {
        super();
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success(data, "success");
    }

    public static <T> Result<T> success(T data, String msg) {
        return success(data, SUCCESS, msg);
    }

    public static <T> Result<T> success(T data, Integer code, String msg) {
        Result<T> resultData = new Result<T>();
        resultData.setCode(code);
        resultData.setMsg(msg);
        resultData.setData(data);
        return resultData;
    }

    public static Result error(String msg) {
        Result resultData = new Result();
        resultData.setCode(ERROR);
        resultData.setMsg(msg);
        return resultData;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
